package com.fantsey.test.mybatis.generator;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;

/**
 * Description:
 *
 * @author fx
 * @date 2023/4/17 15:28
 */
public class MyBatisPlusGenerator {

    public static final String PROJECT_NAME = "test-core/test-core-mybatis-generator";

    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3307/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=round&useSSL=false&allowMultiQueries=true";

        String projectPath = System.getProperty("user.dir") + "/";
        String username = "root";
        String password = "123456";

        DataSourceConfig.Builder dcBuilder = new DataSourceConfig.Builder(url, username, password)
                .dbQuery(new MySqlQuery())
                .schema("mybatis-plus")
                .typeConvert(new MySqlTypeConvert(){
                    @Override
                    public IColumnType processTypeConvert(GlobalConfig config, String fieldType) {
                        String t = fieldType;
                        if (t.toLowerCase().contains("char") || t.toLowerCase().contains("text")) {
                            return DbColumnType.STRING;
                        }
                        if (t.toLowerCase().contains("tinyint")){
                            return DbColumnType.INTEGER;
                        }
                        if (t.toLowerCase().contains("datetime")){
                            return DbColumnType.DATE;
                        }
                        return super.processTypeConvert(config, fieldType);

                    }
                })
                .keyWordsHandler(new MySqlKeyWordsHandler());


        FastAutoGenerator.create(dcBuilder)
                .globalConfig(builder -> {
                    builder.author("fx") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(projectPath+ PROJECT_NAME +"/src/main/java");
                })
                .packageConfig(builder -> {
                    builder.parent("com.fantsey.test") // 设置父包名
                            .moduleName("shortUrl");
                })
                .strategyConfig(builder -> {
                    builder.addTablePrefix("t_");
                    builder.addInclude("t_short_url") // 设置需要生成的表名
                            .entityBuilder()
                            .naming(NamingStrategy.underline_to_camel)
                            .formatFileName("%sEntity")
                            .enableLombok()  // 开启 lombok 模型
                            .enableChainModel(); // 开启链式模型



                })
                .execute();
    }
}
